﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.IO;
using System.Text;

public partial class design_page_student_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_send_post = new DATA_operate();   //创建数据库操作类对象
   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //is_receiver_show.Style.Add("display", "none");
        }
        if (DropDownList1.SelectedItem.Text == "个人邮")
        {
            Label1.Visible = true;
            receiver_id.Visible = true;
            Button1.Visible = true;
        }
        else
        {
            Label1.Visible = false;
            receiver_id.Visible = false;
            Button1.Visible = false;
            Button4_Click(sender, e);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)  //查询
    {
        Button4.Visible = true;
        GridView_show_admin_id.Visible = true;
        GridView_show_student_id.Visible = true;
        GridView_show_teacher_id.Visible = true;

        string receivername=receiver_id.Text;
        string sql_search_from_admin = "select _Aid,_Aname from _admin where _Aname like '%" + receivername + "%'";
        string sql_search_from_teacher = "select _Tid,_Tname from _teacher where _Tname like '%" + receivername + "%'";
        string sql_search_from_student = "select _Sid,_Sname from _student where _Sname like '%" + receivername + "%'";
        data_operate_in_send_post.Data_bind(sql_search_from_admin, GridView_show_admin_id, "_Aid");
        data_operate_in_send_post.Data_bind(sql_search_from_teacher, GridView_show_teacher_id, "_Tid");
        data_operate_in_send_post.Data_bind(sql_search_from_student, GridView_show_student_id, "_Sid");
    }





    protected void Button3_Click(object sender, EventArgs e)  //重置
    {
        TextBox2.Text = "";
        TextBox4.Text = "";
        receiver_id.Text = "ID忘记了。直接输入姓名。点击查询吧";
       
    }
    protected void Button2_Click(object sender, EventArgs e)  //发送
    {
        if(TextBox2.Text=="")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('标题不能为空');", true);
        }
        else if (TextBox4.Text == "")
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('消息不能为空');", true);
        }
        else
        {
            HttpCookie cookie = Request.Cookies["stuid"];
            string str_sid = cookie.Value.ToString();
            string _post_id = Make_tran_postid();
            string _addresser_id = str_sid;
            string _addresser_name = data_operate_in_send_post.dataread("_student", "_Sname", "_Sid", str_sid);
            string _post_name = TextBox2.Text;  //标题
            string _post_content = TextBox4.Text;//内容
            string _send_time = DateTime.Now.ToLocalTime().ToString();

            if (DropDownList1.SelectedItem.Text == "留言板")
            {
                if (data_operate_in_send_post.dataadd("_tran_post", "_post_id,_addresser_id,_addresser_name,_post_name,_post_content,_send_time", _post_id + "," + _addresser_id + "," + _addresser_name + "," + _post_name + "," + _post_content + "," + _send_time))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送成功');", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送失败');", true);
                }
            }
            else
            {
                 string receiverid = receiver_id.Text;
                if(receiverid=="")
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('收件人ID不能为空');", true);
                }
                else
                {
                    //--------------
                    if (!data_operate_in_send_post.Is_exist("_student", "_Sid", receiverid))
                    {
                        if (!data_operate_in_send_post.Is_exist("_admin", "_Aid", receiverid))
                        {
                            if (!data_operate_in_send_post.Is_exist("_teacher", "_Tid", receiverid))
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送失败：收件人ID不正确。请使用查询功能进行查询后再次尝试发送');", true);
                                return;
                            }
                        }
                    }
                    //--------------
                    if (data_operate_in_send_post.dataadd("_tran_post", "_post_id,_addresser_id,_addresser_name,_receiver_id,_post_name,_post_content,_send_time", _post_id + "," + _addresser_id + "," + _addresser_name + "," + receiverid + "," + _post_name + "," + _post_content + "," + _send_time))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送成功');", true);
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('发送失败');", true);
                    }

                 
                }
            }
        }
      
       
    }
    protected void Button4_Click(object sender, EventArgs e)  //关闭
    {
        Button4.Visible = false;
        GridView_show_admin_id.Visible = false;
        GridView_show_student_id.Visible = false;
        GridView_show_teacher_id.Visible = false;
    }



  
    protected void gridview_admin_rowcommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "admin_choose")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_show_admin_id.Rows[index];
            receiver_id.Text = ((Label)gvr.FindControl("dLabel_detail_admid")).Text;  
        }
    }
    protected void gridview_teacher_rowcommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "teacher_choose")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_show_teacher_id.Rows[index];
            receiver_id.Text = ((Label)gvr.FindControl("dLabel_detail_teaid2")).Text;  
     
        }
    }
    protected void GridView_show_student_id_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "student_choose")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_show_student_id.Rows[index];
            receiver_id.Text = ((Label)gvr.FindControl("dLabel_detail_stuid3")).Text;   
        }
    }






    private string Make_tran_postid()   //生成作业ID
    {


        string numA = data_operate_in_send_post.dataread("MAX(_post_id)", "_tran_post", true);   //获得站内邮id
        if (numA == null)
        {
            numA = "00000000000000";
        }
        string numB = "1";   //最大ID 号加1
        numA = numA.Replace(" ", "");
        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }
}